package com.example.freshmenclient.fragments;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.freshmenclient.ClassMateDetailActivity;
import com.example.freshmenclient.R;
import com.example.freshmenclient.adapter.ClassMateAdapter;
import com.example.freshmenclient.bean.ClassMateBean;
import com.example.freshmenclient.util.GainDataUtil;
import com.example.freshmenclient.util.UrlUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;


public class ClassMateFragment extends Fragment {
    private static final String TAG = "ClassMateFragment";

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ListView lvClassMate;
    private ClassMateBean classMateBean;
    private TextView tvClassMateDept;
    private TextView tvClassMateMajor;

    public ClassMateFragment() {
    }


    public static ClassMateFragment newInstance(String param1, String param2) {
        ClassMateFragment fragment = new ClassMateFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_class_mate, container, false);
        lvClassMate = view.findViewById(R.id.lv_classmate);

        tvClassMateDept = view.findViewById(R.id.tv_classmat_dept);
        tvClassMateMajor = view.findViewById(R.id.tv_classmat_major);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String url = UrlUtil.basePath + "freshmenserver/classmate/list";
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("major", UrlUtil.major);

/*            RequestQueue requestQueue = Volley.newRequestQueue(getContext());
            JsonObjectRequest request = new JsonObjectRequest(
                    Request.Method.POST,
                    url,
                    jsonObject,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Gson gson = new Gson();
                            classMateBean = gson.fromJson(jsonObject.toString(), ClassMateBean.class);
                            tvClassMateDept.setText(classMateBean.getData().get(0).getDepartment());
                            tvClassMateMajor.setText(classMateBean.getData().get(0).getMajor());
                            lvClassMate.setAdapter(new ClassMateAdapter(getContext(), classMateBean.getData()));
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                        }
                    }
            );
            requestQueue.add(request);*/


            GainDataUtil.gainDataForPost(getContext(), url, jsonObject
                    , new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            Gson gson = new Gson();
                            classMateBean = gson.fromJson(jsonObject.toString(), ClassMateBean.class);
                            tvClassMateDept.setText(classMateBean.getData().get(classMateBean.getData().size() - 1).getDepartment());
                            tvClassMateMajor.setText(classMateBean.getData().get(classMateBean.getData().size() - 1).getMajor());
                            lvClassMate.setAdapter(new ClassMateAdapter(getContext(), classMateBean.getData()));
                        }
                    }, null);

            showClassMateDetails();


           /* JSONObject jsonObject1 = GainDataUtil.gainDataForPost(getContext(), url, jsonObject);
            Log.e(TAG, "onActivityCreated: " + jsonObject1);
            if (jsonObject1 != null) {
                Gson gson = new Gson();
                classMateBean = gson.fromJson(jsonObject1.toString(), ClassMateBean.class);
                tvClassMateDept.setText(classMateBean.getData().get(0).getDepartment());
                tvClassMateMajor.setText(classMateBean.getData().get(0).getMajor());
                lvClassMate.setAdapter(new ClassMateAdapter(getContext(), classMateBean.getData()));
            }*/


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void showClassMateDetails() {
        lvClassMate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //保存用户Id
                UrlUtil.userId = classMateBean.getData().get(position).getId();
                Log.e(TAG, "onItemClick: " + UrlUtil.userId);
                startActivity(new Intent(getContext(), ClassMateDetailActivity.class));
            }
        });
    }
}
